package com.fendaren.controller;

import com.fendaren.pojo.Items;
import com.fendaren.pojo.ItemsImg;
import com.fendaren.pojo.ItemsParam;
import com.fendaren.pojo.ItemsSpec;
import com.fendaren.pojo.vo.CommentLevelCountsVO;
import com.fendaren.pojo.vo.ItemInfoVO;
import com.fendaren.pojo.vo.ShopcartVO;
import com.fendaren.service.ItemService;
import com.fendaren.utils.JSONResult;
import com.fendaren.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("items")
public class ItemsController extends BaseController {

    @Autowired
    private ItemService itemService;

    @GetMapping("/info/{itemId}")
    public JSONResult info(@PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return JSONResult.errorMsg(null);
        }

        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> ItemImgList = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(ItemImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);

        return JSONResult.ok(itemInfoVO);
    }

    @GetMapping("/commentLevel")
    public JSONResult commentLevel(@RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return JSONResult.errorMsg(null);
        }
        CommentLevelCountsVO countsVO = itemService.queryCommentLevelCounts(itemId);
        return JSONResult.ok(countsVO);
    }

    @GetMapping("/comments")
    public JSONResult comments(@RequestParam String itemId,
                               @RequestParam Integer level,
                               @RequestParam Integer page,
                               @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(itemId)) {
            return JSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult grid = itemService.queryPagedComments(itemId, level, page, pageSize);
        return JSONResult.ok(grid);

    }

    @GetMapping("/search")
    public JSONResult search(@RequestParam String keywords,
                             @RequestParam String sort,
                             @RequestParam Integer page,
                             @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return JSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }

        PagedGridResult grid = itemService.searchItems(keywords, sort, page, pageSize);
        return JSONResult.ok(grid);
    }

    @GetMapping("/catItems")
    public JSONResult catItems(@RequestParam Integer catId,
                               @RequestParam String sort,
                               @RequestParam Integer page,
                               @RequestParam Integer pageSize) {
        if (catId == null) {
            return JSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult list = itemService.searchItems(catId, sort, page, pageSize);
        return JSONResult.ok(list);

    }

    @GetMapping("/refresh")
    public JSONResult refresh(@RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return JSONResult.ok();
        }
        List<ShopcartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
        return JSONResult.ok(list);
    }
}
